import Base from "../base.js";
import theatersService from"../../service/theatersService.js"
export default class extends Base {

  constructor(props) {
    super(props);
    this.updata=theatersService.getTheater()//拿到保存到本地服务器的数据
  }

  render() {
    const template = `
    <form class="layui-form" action="">
    <div class="layui-form-item">
      <label class="layui-form-label">影院</label>
      <div class="layui-input-inline">
      <input type="text" name="title" required  lay-verify="required" readonly class="layui-input" value="${this.updata.cinemasId.name}">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">原影厅名</label>
      <div class="layui-input-inline addTheaters" style="width:300px">
        <input type="text" name="name" class="layui-input layui-input-inline" value="${this.updata.name}">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">新影厅名</label>
      <div class="layui-input-inline addTheaters" style="width:300px">
        <input type="text" name="name"  placeholder="请输入影厅名" class="layui-input layui-input-inline">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">影厅状态</label>
      <div class="layui-input-block">
        <input type="radio" name="status" value=true title="营业" class="on">
        <input type="radio" name="status" value=false title="未营业" class="off">
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn upTheaterBtn" lay-submit lay-filter="formDemo" type="button">保存</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>
        `
    this.$el.html(template);
    this.rerender();
  }
  //向服务器发送请求
  async updateOneTheater(data,_id){
    const result= await theatersService.updateOneTheater(data,_id);
  if (result) {//如果返回ok则弹出提示框
    layui.layer.msg('更新成功');}
};

  rerender(){
    if (this.updata.status) {
      $(".on").attr("checked",true);
    }else{
      $(".off").attr("checked",true);
    }
  }

  handle(){
    var that=this;
    //更新按钮点击事件
    that.form.on('submit(formDemo)', function(data){
      that.updateOneTheater(data.field,that.updata._id)//传了要更新的那条数据的_ID和更改的内容
      return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });   
}
  layui(){
    this.form.render()
  };
}